package com.buba.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.buba.mapper.LgAuthortiyMapper;
import com.buba.pojo.LgAuthortiy;
import com.buba.pojo.LgRoleAuthority;
import com.buba.mapper.LgRoleAuthorityMapper;
import com.buba.pojo.other.LeftTree;
import com.buba.service.LgRoleAuthorityService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author MrJia
 * @since 2020-10-27
 */
@Service
public class LgRoleAuthorityServiceImpl extends ServiceImpl<LgRoleAuthorityMapper, LgRoleAuthority> implements LgRoleAuthorityService {

    private List<LgAuthortiy> lgAuthortiys;
    @Autowired
    private LgRoleAuthorityMapper lgRoleAuthorityMapper;
    @Autowired
    private LgAuthortiyMapper lgAuthortiyMapper;

    @Override
    public List<LeftTree> selectListByRoleId(String roleId) {
        QueryWrapper<LgRoleAuthority> lgRoleAuthorityQueryWrapper = new QueryWrapper<>();
        List<LgRoleAuthority> lgRoleAuthorities = lgRoleAuthorityMapper.selectList(lgRoleAuthorityQueryWrapper.lambda().eq(LgRoleAuthority::getRoleId, roleId));
        lgAuthortiys = new ArrayList<>();
        for (LgRoleAuthority lgRoleAuthority : lgRoleAuthorities) {
            lgAuthortiys.add(lgAuthortiyMapper.selectById(lgRoleAuthority.getAuthId()));
        }
        return initLeftTree(0L);
    }

    private List<LeftTree> initLeftTree(Long pid) {
        List<LeftTree> leftTrees = new ArrayList<>();
        for (LgAuthortiy lgAuthortiy : lgAuthortiys) {
            if (Long.parseLong(lgAuthortiy.getPid()) != pid) continue;
            LeftTree leftTree = new LeftTree();
            leftTree.setId(lgAuthortiy.getId());
            leftTree.setRoleName(lgAuthortiy.getRoleName());
            leftTree.setRoleUrl(lgAuthortiy.getRoleUrl());
            leftTree.setIcon(lgAuthortiy.getIcon());
            leftTree.setChildren(initLeftTree(Long.parseLong(lgAuthortiy.getId())));
            leftTrees.add(leftTree);
        }
        return leftTrees;
    }
}
